using ImageCSharp.ViewModels;
using Microsoft.Extensions.DependencyInjection;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;

namespace ImageCSharp.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow(MainViewModel viewModel)
        {
            InitializeComponent();
            DataContext = viewModel;

            // 设置窗口属性
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            
            // 添加快捷键处理
            AddHandler(Keyboard.KeyDownEvent, (KeyEventHandler)HandleKeyDown, true);
        }

        private void HandleKeyDown(object sender, KeyEventArgs e)
        {
            if (DataContext is not MainViewModel vm) return;

            // 处理快捷键
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                switch (e.Key)
                {
                    case Key.N:
                        vm.NewDocumentCommand.Execute(null);
                        e.Handled = true;
                        break;
                    case Key.O:
                        vm.OpenFileCommand.Execute(null);
                        e.Handled = true;
                        break;
                    case Key.S:
                        // 判断 Control + Shift 同时按下（位运算组合 + 位与验证）
                        if ((Keyboard.Modifiers & (ModifierKeys.Control | ModifierKeys.Shift)) == (ModifierKeys.Control | ModifierKeys.Shift))
                        {
                            vm.SaveFileAsCommand.Execute(null);
                        }
                        else
                        {
                            vm.SaveFileCommand.Execute(null);
                        }
                        e.Handled = true;
                        break;
                    case Key.Z:
                        vm.UndoCommand.Execute(null);
                        e.Handled = true;
                        break;
                    case Key.Y:
                        vm.RedoCommand.Execute(null);
                        e.Handled = true;
                        break;
                    case Key.Add:
                    case Key.OemPlus:
                        vm.ZoomInCommand.Execute(null);
                        e.Handled = true;
                        break;
                    case Key.Subtract:
                    case Key.OemMinus:
                        vm.ZoomOutCommand.Execute(null);
                        e.Handled = true;
                        break;
                    case Key.D0:
                        vm.ActualSizeCommand.Execute(null);
                        e.Handled = true;
                        break;
                }
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            // 检查是否有未保存的更改
            if (DataContext is MainViewModel vm && vm.CurrentDocument?.IsModified == true)
            {
                var result = MessageBox.Show(
                    "您有未保存的更改。是否保存？",
                    "Image-CSharp",
                    MessageBoxButton.YesNoCancel,
                    MessageBoxImage.Question);

                switch (result)
                {
                    case MessageBoxResult.Yes:
                        vm.SaveFileCommand.Execute(null);
                        break;
                    case MessageBoxResult.Cancel:
                        e.Cancel = true;
                        return;
                }
            }

            base.OnClosing(e);
        }
    }
}